package com.uglyboys.control;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.Drawable;

import com.uglyboys.constantes.Constantes;
import com.uglyboys.customtweetreader.model.CustomTweet;
import com.uglyboys.customtweetreader.reader.CustomTweetReader;
import com.uglyboys.model.Brother;
import com.uglyboys.model.Frase;

public class BBBController {

	private static BBBController instance = null;
	private Drawable imagem;
	private CustomTweet customTweet;
	private Brother brother;
	private List<Brother> brothers;
	
	public Brother getBrother() {
		return brother;
	}

	public void setBrother(Brother brother) {
		this.brother = brother;
	}

	public CustomTweet getCustomTweet() {
		return customTweet;
	}

	public void setCustomTweet(CustomTweet customTweet) {
		this.customTweet = customTweet;
	}

	public Drawable getImagem() {
		return imagem;
	}

	public void setImagem(Drawable imagem) {
		this.imagem = imagem;
	}

	private BBBController() {
	}

	public List<Brother> getBrothers() {
		return brothers;
	}

	public void setBrothers(List<Brother> brothers) {
		this.brothers = brothers;
	}

	public static BBBController getInstance() {
		if (instance == null) {
			instance = new BBBController();
		}
		return instance;
	}

	public List<CustomTweet> getTweets(int page, String... hashtags) {
		List<CustomTweet> customTweets = new ArrayList<CustomTweet>();
		try {
			customTweets = CustomTweetReader.getTweets(page, hashtags);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return customTweets;
	}

	public List<CustomTweet> getTimeLine(int page, String idUser) {
		List<CustomTweet> customTweets = new ArrayList<CustomTweet>();
		try {
			customTweets = CustomTweetReader.getTimeLine(page, idUser);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return customTweets;
	}
	
	
	public void getInfoBrother(Brother brother) throws IOException{
			URL oracle = new URL(Constantes.SITE_BROTHER + brother.getUrl());
			URLConnection yc;
			yc = oracle.openConnection();
	        BufferedReader in = new BufferedReader(
	                                new InputStreamReader(
	                                yc.getInputStream()));
	        String inputLine;
	        StringBuilder result = new StringBuilder();
	        while ((inputLine = in.readLine()) != null){
	        	result.append(inputLine);
	        }
	        in.close();
	        
	        String resultadoFinal = result.toString();
			realizarTratamento(resultadoFinal, brother);
		
//		
//		
//		try {
//			HttpClient httpClient = new DefaultHttpClient();
//			HttpContext localContext = new BasicHttpContext();
//			HttpGet httpGet = new HttpGet(Constantes.SITE_BROTHER + brother.getNome());
//			HttpResponse response;
//				response = httpClient.execute(httpGet, localContext);
//			StringBuilder result = new StringBuilder();
//			 
//			BufferedReader reader = new BufferedReader(
//			    new InputStreamReader(
//			      response.getEntity().getContent()
//			    )
//			  );
//			 
//			String line = null;
//			while ((line = reader.readLine()) != null){
//				result.append(line + "\n");
//			}
			
//			String resultadoFinal = result.toString();
//			realizarTratamento(resultadoFinal, brother);
//		} catch (ClientProtocolException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}

	private void realizarTratamento(String resultadoFinal, Brother brother) {
		extrairFrases(resultadoFinal, brother);
		extrairStatus(resultadoFinal, brother);
		
	}

	private void extrairStatus(String resultadoFinal, Brother brother) {
		String[] partesStatus = resultadoFinal.split("<ul class=\"status-participante\">");
        int index = 0;
        for(int i = 1; i < partesStatus.length; i++){
        	String statusComoTabela = partesStatus[i].split("</ul>")[0];
        	for(int j = 0; j < 3; j++){
	        	String substring = statusComoTabela.substring(statusComoTabela.indexOf("<strong>", index)+"<strong>".length(), statusComoTabela.indexOf(" ", statusComoTabela.indexOf("<strong>", index)));
	        	index = statusComoTabela.indexOf("<strong>", index)+1;
				switch (j) {
				case 0:
					brother.setQtdVezesParedao(Integer.parseInt(substring));
					break;
				case 1:
					brother.setQtdVezesAnjo(Integer.parseInt(substring));
					break;
				case 2:
					brother.setQtdVezesLider(Integer.parseInt(substring));
					break;

				}
        	}
		}
		
	}

	private void extrairFrases(String resultadoFinal, Brother brother) {
		String[] partesPost = resultadoFinal.split("<h6></h6>        <blockquote>            <span class=\"post-corpo-trecho\">");
		for (int i = 1; i < partesPost.length; i++) {
			Frase frase = new Frase();
			frase.setFrase(partesPost[i].split("</span>")[0].trim().replace("&quot;", ""));
			brother.getFrases().add(frase);
		}
	}
		

}
